package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC29 {

    public int divide(int dividend, int divisor) {
        if (dividend == 0 || divisor == 0)
            return 0;
        boolean flag = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
        long a = Math.abs((long) dividend);//注意要转换成long型
        long b = Math.abs((long) divisor);
        long result = 0;
        if (a < b)
            return 0;
        long sum = 0;
        long pow = 0;
        while (a >= b) {
            sum = b;
            pow = 1;
            while (sum + sum <= a) {
                sum += sum;
                pow += pow;
            }
            a -= sum;
            result += pow;
        }

        result = (int) (flag == true ? -result : result);
        if (result == Integer.MIN_VALUE && !flag) {//overflow for int
            return Integer.MAX_VALUE;
        }

        return (int) result;
    }

    public static void main(String[] args) {
        LC29 an = new LC29();
//        System.out.println(an.divide(-2147483648, 1));

        System.out.println(Integer.MIN_VALUE);
    }
}
